from django.conf.urls import patterns, include, url
from django.http import *
import os, sys, imp
from utils.utils import *
import traceback
from website.settings import LOGGER as logger
urlpatterns = patterns('',
    url(r'^(.*)$', 'trp.urls.forward'),
)

# define url 

path = os.path.join(os.path.dirname(__file__), 'views')

def forward(request, url):
    urls = remove_none(unicode.split(url, '/'))
    logger.info(urls)
    if len(urls) == 0:
        urls.append('index')

    if urls[0] == '':
        urls[0] = 'index'

    if len(urls) == 1:
        urls.append('main')

    if urls[1] == '':
        urls[1] = 'main'

    if len(urls) != 2:
        raise Http404

    try :
        f, filename, desc = imp.find_module(urls[0], [path])
        module = imp.load_module(urls[0], f, filename, desc)
        method = module.__dict__.get(urls[1])
        response = method(request)
        return response
    except Exception as e:
        logger.exception(e)
        raise Http404

